package com.xbfinance.manager.service.impl;

import com.xbfinance.manager.dao.P2pAccountRechargeMapper;
import com.xbfinance.manager.dao.P2pAccountRechargeMapperSon;
import com.xbfinance.manager.pojo.dto.AdminPageParam;
import com.xbfinance.manager.pojo.dto.AdminPageResult;
import com.xbfinance.manager.pojo.dto.AdminSearch;
import com.xbfinance.manager.pojo.po.P2pAccountRecharge;
import com.xbfinance.manager.pojo.po.P2pAccountRechargeExample;
import com.xbfinance.manager.service.AdminRechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AdminRechargeServiceImpl implements AdminRechargeService {
    @Autowired
    private P2pAccountRechargeMapper p2pAccountRechargeMapper;
    @Autowired
    private P2pAccountRechargeMapperSon p2pAccountRechargeMapperSon;

    @Override
    public AdminPageResult<P2pAccountRecharge> listRecharge(AdminPageParam page) {
        AdminPageResult<P2pAccountRecharge> res = new AdminPageResult<>();
        //总数
        int total = p2pAccountRechargeMapper.countByExample(null);
        res.setTotal(total);
        //行数
        int rows = page.getRows();
        res.setRows(rows);
        //当前页
        res.setPage(page.getPage());
        //总页数
        int pages= (int) Math.ceil(1.0*total/rows);
        res.setPages(pages);
        //数据
        List<P2pAccountRecharge> list = p2pAccountRechargeMapperSon.selectByPage(page);
        res.setDocs(list);

        return res;
    }

    @Override
    public int agreeRecharge(int rid) {
        P2pAccountRecharge p = new P2pAccountRecharge();
        p.setRid(rid);
        p.setVerifyuser(888);
        p.setStatus((byte)1);
        p.setVerifytime(new Date());

        return p2pAccountRechargeMapperSon.updateByPrimaryKeySelective(p);
    }

    @Override
    public int adminRejectRecharge(int rid) {
        P2pAccountRecharge p = new P2pAccountRecharge();
        p.setRid(rid);
        p.setVerifyuser(888);
        p.setStatus((byte)2);
        p.setVerifytime(new Date());
        return p2pAccountRechargeMapperSon.updateByPrimaryKeySelective(p);

    }

    @Override
    public int removeRechargeByRid(int rid) {
        return p2pAccountRechargeMapperSon.deleteByPrimaryKey(rid);
    }

    @Override
    public AdminPageResult<P2pAccountRecharge> searchRecharge(AdminPageParam page, AdminSearch as) {
        AdminPageResult<P2pAccountRecharge> res = new AdminPageResult<>();
        int rows = page.getRows();
        res.setRows(rows);
        int total = p2pAccountRechargeMapperSon.countByExample(null);
        res.setTotal(total);
        int pages = (int)Math.ceil(1.0*total/rows);
        res.setPages(pages);
        res.setPage(page.getPage());
        List<P2pAccountRecharge> list = p2pAccountRechargeMapperSon.selectPageByRid(page,as);
        res.setDocs(list);
        return res;
    }

    @Override
    public int adminRemoves(List<Integer> list) {
        P2pAccountRechargeExample example = new P2pAccountRechargeExample();
        example.createCriteria().andRidIn(list);
        return p2pAccountRechargeMapperSon.deleteByExample(example);

    }


}
